package com.hgzy.sys.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hgzy.sys.common.Constant;
import com.hgzy.sys.common.DataGridView;
import com.hgzy.sys.domain.Notice;
import com.hgzy.sys.domain.User;
import com.hgzy.sys.service.NoticeService;
import com.hgzy.sys.vo.LogInfoVo;
import com.hgzy.sys.vo.NoticeVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 管理公告的控制层
 * @author : look-word
 * @date : 2022-01-15 17:48
 **/
@Slf4j
@RestController
@RequestMapping("/notice")
public class NoticeController  extends BaseController{
    @Autowired
    private NoticeService noticeService;

    /**
     * 查询所有通知
     * @return
     */
    @RequestMapping("loadAllNotice")
    public DataGridView loadAllNotice(NoticeVo noticeVo){
        IPage<Notice> page = new Page<>(noticeVo.getPage(),noticeVo.getLimit());
        QueryWrapper<Notice> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(noticeVo.getTitle()),"title",noticeVo.getTitle());
        wrapper.like(StringUtils.isNotBlank(noticeVo.getOpername()),"opername",noticeVo.getOpername());
        wrapper.ge(noticeVo.getStartTime()!=null,"createtime",noticeVo.getStartTime());
        wrapper.le(noticeVo.getEndTime()!=null,"createtime",noticeVo.getEndTime());
        wrapper.orderByDesc("createtime");
        noticeService.page(page,wrapper);
        return new DataGridView(page.getTotal(),page.getRecords());
    }

    /**
     *根据指定id 删除 登陆日志
     */
    @RequestMapping("deleteNotice")
    public DataGridView deleteLoginfo(Integer id){
        log.info(id+"");
        boolean removeById = noticeService.removeById(id);
        return new DataGridView(Constant.OK,removeById?Constant.DELETE_OK_MSG:Constant.DELETE_ERROR_MSG);
    }
    /**
     * 根据id批量删除
     */
    @RequestMapping("batchDeleteNotice")
    public DataGridView batchDeleteById(NoticeVo noticeVo){
        Integer[] ids = noticeVo.getIds();
        log.info(ids+"");
        boolean removeById = noticeService.removeByIds(Arrays.asList(ids));
        return new DataGridView(Constant.OK,removeById?Constant.DELETE_OK_MSG:Constant.DELETE_ERROR_MSG);
    }
    /**
     * 添加系统公告
     */
    @RequestMapping("addNotice")
    public DataGridView addNotice(NoticeVo noticeVo, HttpSession session){
        // 获取当前登陆的用户
        User user = bySessionGetUser(session);
        // 文章标题
        String title = noticeVo.getTitle();
        // 文章内容
        String content = noticeVo.getContent();
        Notice entity = new Notice(title,content,new Date(),user.getName());
        boolean save = noticeService.save(entity);
        return new DataGridView(Constant.OK,save?Constant.ADD_OK_MSG:Constant.ADD_ERROR_MSG);
    }

    /**
     * 更新系统公告
     */
    @RequestMapping("updateNotice")
    public DataGridView updateNotice(Notice notice, HttpSession session){
        log.info(notice.toString());
        // 获取当前登陆的用户
        User user = bySessionGetUser(session);
        // 文章标题
        String title = notice.getTitle();
        // 文章内容
        String content = notice.getContent();
        Notice entity = new Notice(title,content,new Date(),user.getName());
        // 条件构造器比对
        UpdateWrapper<Notice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id",notice.getId());
        boolean update = noticeService.update(entity, updateWrapper);
        return new DataGridView(Constant.OK,update?Constant.UPDATE_OK_MSG:Constant.UPDATE_ERROR_MSG);
    }

}
